import takeGoods from "./components/takeGoods.vue";
import DList from "@/components/dlist";
export default {
  data() {
    return {
      goods_name: "",
      popupConsignmentShow: false,
      takeGoodsShow: false,
      popupPlatformShow: false,
      name: "",
      listData: [],
      loading: false,
      finished: false,
      page: 1,
      lock: 0,
      manage_id: "", //我的存货列表的ID
      manage_goods: {}, //提货-弹窗数据
      // now: String(Date.parse(new Date())).substring(0, 10),
      is_sign: false, //为ture 请求电子合同
      order_id: ""
    };
  },
  activated() {
    this.name = this.$route.params.name;
    this.fun.setWXTitle(this.$route.params.name);
    this.onSearch();
  },
  methods: {
    async getData() {
      let url = this.name == "代销管理" ? "plugin.stock-service.frontend.controllers.help.index" : this.name == "我的存货" ? "plugin.stock-service.frontend.controllers.stock.index" : "";
      if (this.loading === true || this.finished === true) {
        return;
      }
      this.loading = true;
      let { data, result, msg } = await $http.post(url, { page: this.page, goods_name: this.goods_name });
      if (result === 0) {
        this.$toast(msg);
        return;
      }
      this.lock = data.lock;
      this.is_sign = data.is_sign;
      if (data.list.data.length === 0 || data.list.data.length < data.list.per_page || data.list.current_page === data.list.last_page) {
        this.finished = true;
      }
      this.listData.push(...data.list.data);
      this.page++;
      this.loading = false;
    },
    onLoad() {
      this.getData();
    },
    // 搜索
    onSearch() {
      this.loading = false;
      this.finished = false;
      this.page = 1;
      this.listData = [];
      this.getData();
    },
    // 代销-弹窗
    openConsignment(id, type, order_id) {
      if (!type) return;
      this.manage_id = id;
      this.order_id = order_id;
      this.popupConsignmentShow = true;
    },
    // 确认代销
    async sureConsignment() {
      if (this.is_sign) {
        //电子合同
        $http.get("plugin.shop-esign.frontend.contract.get-contract", { activity_id: this.order_id, plugin_name: "stock-service" }, "...").then(response => {
          if (response.result === 1) {
            if (response.data.need_sign === 1 && response.data.contract_id) {
              this.$dialog
                .alert({ message: `代销前需要签署电子合同` })
                .then(() => {
                  this.$router.push(this.fun.getUrl("signPagePlu", { id: response.data.contract_id }, { fromOrder: 1 }));
                })
                .catch(() => {});
            }else {
              this.asyncConsignment();
            }
          } else {
            this.$toast(response.data.msg);
          }
        });
      } else {
        this.asyncConsignment();
      }
    },
    async asyncConsignment () {
      let { data, result, msg } = await $http.post("plugin.stock-service.frontend.controllers.stock.help", { manage_id: this.manage_id });
      this.$toast(msg);
      if (result === 0) {
        return;
      }
      this.popupConsignmentShow = false;
      this.onSearch();
    },
    // 提货弹窗
    openTakeGoodsShow(item, type) {
      console.log(item, "itemitem");
      if (!type) return;
      this.takeGoodsShow = true;
      this.manage_goods = item;
    },
    // 平台回购
    openPlatformShow(id, type) {
      if (!type) return;
      this.manage_id = id;
      this.popupPlatformShow = true;
    },
    // 确定平台回购
    async surePlatform() {
      let { data, result, msg } = await $http.post("plugin.stock-service.frontend.controllers.help.back", { manage_id: this.manage_id });
      this.$toast(msg);
      if (result === 0) {
        return;
      }
      this.popupPlatformShow = false;
      this.onSearch();
    },
    finishTime(index) {
      // let time = String(Date.parse(new Date())) * 1 / 1000
      // console.log(this.listData[index].lock_time,111,time);
      // this.listData[index].take_button = this.listData[index].lock_time <= time ? true : this.listData[index].take_button;
      // this.listData[index].back_button = this.listData[index].lock_time <= time ? true : this.listData[index].back_button;
    }
  },
  components: {
    takeGoods,
    DList
  }
};
